<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>iScroll demo: simple</title>
<script type="text/javascript" src="js/lib/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="js/lib/jquery.cvloader.js"></script>
<script type="text/javascript" src="http://cubiq.org/dropbox/iscroll4/src/iscroll.js"></script>

<script type="text/javascript">

var myScroll;
function loaded() {
	myScroll = new iScroll('wrapper',{
		onScrollStart:function(e){
			if(iscroll.isLoading){
				return;
			}

			var $last=$list.find("li:last"),
				$first=$list.find("li:first");

			var pidx=h0=y0=0;
			y0=$last.data("y");
			//h0=-parseInt(y0)-h+window.innerHeight;
			h0=-parseInt(y0)+window.innerHeight*1.5;

			if( this.y<h0){
				pidx=parseInt($last.data("pidx"))+1;
				if(pidx>total) return;
				//console.log("load next page");
				$("#tip").html(h0);
				iscroll.getPagedData(pidx,true,null);
				return;
			}

			y0=$first.data("y");
			h0=-parseInt(y0)-window.innerHeight*1.2;
			if( this.y > h0 ){
				
				pidx=parseInt($first.data("pidx"))-1;
				if(pidx<1) return;
				//console.log("load previous page");
				$("#tip").html(h0);
				iscroll.getPagedData(pidx,false,null);
			}
		},
		onScrollEnd:function(e){
			if(iscroll.isLoading){
				return;
			}

			var $last=$list.find("li:last"),
				$first=$list.find("li:first");

			var pidx=h0=y0=0;
			y0=$last.data("y");
			//h0=-parseInt(y0)-h+window.innerHeight;
			h0=-parseInt(y0)+window.innerHeight*1.5;

			if( this.y<h0){
				
				pidx=parseInt($last.data("pidx"))+1;
				if(pidx>total) return;
				//console.log("load next page");
				$("#tip").html(h0);
				iscroll.getPagedData(pidx,true,null);
				return;
			}

			y0=$first.data("y");
			h0=-parseInt(y0)-window.innerHeight*1.2;
			if( this.y > h0 ){
				
				pidx=parseInt($first.data("pidx"))-1;
				if(pidx<1) return;
				//console.log("load previous page");
				$("#tip").html(h0);
				iscroll.getPagedData(pidx,false,null);
			}
		}
	});
}

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);

</script>

<style type="text/css" media="all">
body,ul,li {
	padding:0;
	margin:0;
	border:0;
}

body {
	font-size:12px;
	-webkit-user-select:none;
    -webkit-text-size-adjust:none;
	font-family:helvetica;
}

#header {
	position:absolute; z-index:2;
	top:0; left:0;
	width:100%;
	height:45px;
	line-height:45px;
	background-color:#d51875;
	background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #fe96c9), color-stop(0.05, #d51875), color-stop(1, #7b0a2e));
	background-image:-moz-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);
	background-image:-o-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);
	padding:0;
	color:#eee;
	font-size:20px;
	text-align:center;
}

#header a {
	color:#f3f3f3;
	text-decoration:none;
	font-weight:bold;
	text-shadow:0 -1px 0 rgba(0,0,0,0.5);
}

#footer {
	position:absolute; z-index:2;
	bottom:0; left:0;
	width:100%;
	height:48px;
	line-height:48px;
	background-color:#222;
	background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #999), color-stop(0.02, #666), color-stop(1, #222));
	background-image:-moz-linear-gradient(top, #999, #666 2%, #222);
	background-image:-o-linear-gradient(top, #999, #666 2%, #222);
	padding:0;
	border-top:1px solid #444;
	text-align:center;
	color:#f4f4f4;
}

#wrapper {
	position:absolute; z-index:1;
	top:45px; bottom:48px; left:0;
	width:100%;
	background:#aaa;
	overflow:auto;
}

#scroller {
	position:absolute; z-index:1;
/*	-webkit-touch-callout:none;*/
	-webkit-tap-highlight-color:rgba(0,0,0,0);
	width:100%;
	padding:0;
}

#scroller ul {
	list-style:none;
	padding:0;
	margin:0;
	width:100%;
	text-align:left;
	position:relative;
}

#scroller li {
	padding:0 10px;
	height:80px;
	line-height:80px;
	border-bottom:1px solid #ccc;
	border-top:1px solid #fff;
	background-color:#fafafa;
	font-size:14px;
	position:absolute;
	top:0;
	left:0;
	width:100%;
}

#myFrame {
	position:absolute;
	top:0; left:0;
}


</style>
</head>
<body>

<div id="header"><a href="javascript://">iScroll</a></div>
<div id="wrapper">
	<div id="scroller">
		<ul id="thelist">
			<li data-pidx="1">Pretty row 1</li>
			<li data-pidx="1">Pretty row 2</li>
			<li data-pidx="1">Pretty row 3</li>
			<li data-pidx="1">Pretty row 4</li>
			<li data-pidx="1">Pretty row 5</li>
			<li data-pidx="1">Pretty row 6</li>
			<li data-pidx="1">Pretty row 7</li>
			<li data-pidx="1">Pretty row 8</li>
			<li data-pidx="1">Pretty row 9</li>
			<li data-pidx="1">Pretty row 10</li>
			<li data-pidx="2">Pretty row 11</li>
			<li data-pidx="2">Pretty row 12</li>
			<li data-pidx="2">Pretty row 13</li>
			<li data-pidx="2">Pretty row 14</li>
			<li data-pidx="2">Pretty row 15</li>
			<li data-pidx="2">Pretty row 16</li>
			<li data-pidx="2">Pretty row 17</li>
			<li data-pidx="2">Pretty row 18</li>
			<li data-pidx="2">Pretty row 19</li>
			<li data-pidx="2">Pretty row 20</li>
		</ul>
	</div>
</div>
<div id="footer">
	iscroll限容测试<span id="tip"></span>
</div>

<script type="text/javascript">
	//测试限容iscroll

	var evalTpl = function(str, data) {
		var result;
		var patt = new RegExp("%([a-zA-z0-9]+)%");
		while ((result = patt.exec(str)) != null) {
			var v = data[result[1]] || '';
			str = str.replace(new RegExp(result[0], "g"), v);
		};
		return str;
	};

	var h=82,//每条数据的高度
		total=500,//记录总数
		rowPP=10,//每页记录数
		limit=30,
		tpl_item='<li data-pidx="%pidx%" data-y="%y%" style="-webkit-transform:translatey(%y%px);transform:translatey(%y%px);">Pretty row %t%</li>',
		h1=0,
		h2=0,
		$list=$("#thelist");

	//console.log($("#thelist").height());

	//设置总高度
	$list.height(h*total);

	var iscroll={
		isLoading:false,
		init:function(){
			$list.find("li").each(function(i,o){
				$(o).css({
					"-webkit-transform":"translatey("+h*i+"px)",
					"transform":"translatey("+h*i+"px)"
				}).attr("data-y",h*i);
				h1+=h;
			});

			this.initEvt();

		},//init
		initEvt:function(){
			//blabla
			//console.log(window.innerHeight);
		},//initEvt
		fixItems:function(isNext){
			if($list.children().length<=limit) return;


			var pidx=isNext?$list.find("li:first").data("pidx"):$list.find("li:last").data("pidx");
			pidx=parseInt(pidx);

			$list.find('[data-pidx="'+pidx+'"]').remove();

		},//fixItems
		getPagedData:function(pidx,isNext,cbk){
			this.isLoading=true;
			var items=[];
			for(var i=0;i<rowPP;i++){
				items[i]=evalTpl(tpl_item,{
					"pidx":pidx,
					"y":((pidx-1)*rowPP+i)*h,
					"t":((pidx-1)*rowPP+i+1)
				});
			}

			if(isNext){
				$list.append(items.join(""));
			}else{
				$list.prepend(items.join(""));
			}

			this.isLoading=false;

			this.fixItems(isNext);

			if(cbk){
				cbk.call(this);
			}

		}//getPagedData
	};

	iscroll.init();

	//jquery.cvloader test
	$("#header").cvloader({
		color:[251,251,251],
		autoRun:true,
		numPetal:10,
		height:20
	});

	$.fn.cvloader.Detach($("#header"));

</script>

</body>
</html>
